<?php

/**
 * This is the model class for table "{{partner}}".
 *
 * The followings are the available columns in table '{{partner}}':
 * @property string $id
 * @property string $name
 * @property string $link
 * @property string $image_link
 * @property string $sort
 * @property integer $visible
 */
class Partner extends CActiveRecord
{
	const THUMB_PATH = 'upload/partner';
	/**
	 * Returns the static model of the specified AR class.
	 * @return Partner the static model class
	 */
	public static function model($className=__CLASS__) {return parent::model($className);}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {return '{{partner}}';}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// Define only rules for those attributes that will receive user inputs.
		return array(
			array('name, link, image_link, sort, visible', 'required'),
			array('visible', 'numerical', 'integerOnly'=>true),
			array('name, image_link', 'length', 'max'=>500),
			array('link', 'length', 'max'=>255),
			array('sort', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, link, image_link, sort, visible', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 	=> Yii::t('partner', 'ID'),
			'name' 	=> Yii::t('partner', 'Tên đối tác'),
			'link' 	=> Yii::t('partner', 'Link'),
			'image_link' 	=> Yii::t('partner', 'Hình ảnh'),
			'sort' 	=> Yii::t('partner', 'Thứ tự'),
			'visible' 	=> Yii::t('partner', 'Ẩn hiện'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($cri=null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id,true);
		$criteria->compare('t.name',$this->name,true);
		$criteria->compare('t.link',$this->link,true);
		$criteria->compare('t.image_link',$this->image_link,true);
		$criteria->compare('t.sort',$this->sort,true);
		$criteria->compare('t.visible',$this->visible);
		if($cri!==null) $criteria->mergeWith($cri);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array('defaultOrder'=>'t.sort ASC, t.id DESC'),
			'pagination'=>array('pageSize'=>Yii::app()->session->get('backendPageSize',25)),
		));
	}

	public function getThumbS() 
	{

		$str = str_replace('.','_S.', $this->image_link);
		if(file_exists($this->image_link))
		{
			if(!file_exists($str))
			{
				NImage::resizeAndCropRect($this->image_link, $str,119,38);
			}
		}
		return $str;
	}

	public function afterDelete()
	{
		@unlink($this->image_link);
		@unlink($this->thumbS);
		
		parent::afterDelete();
	}
}